package com.example.demo;


import com.example.demo.domain.Bocb2e;
import com.example.demo.domain.Head;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @author Administrator
 * @create 2018/4/8
 * @modify
 */
public class XmlParse {
    public static void main(String[] args){
        // 字符串转XML
        String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<bocb2e version=\"120\" locale=\"zh_CN\"> \n" +
                "  <he-ad> \n" +
                "    <termid>E192168010030</termid>  \n" +
                "    <trnid>04051003129</trnid>  \n" +
                "    <custid>133795318</custid>  \n" +
                "    <cusopr>136140255</cusopr>  \n" +
                "    <trncod>b2e0035</trncod>  \n" +
                "    <token>f0LN_CaY4HreCH53FSKVR3v</token> \n" +
                "  </he-ad>  \n" +
                "</bocb2e>\n";

        Bocb2e bocb2e=new Bocb2e();
        /**
         * 实例Xstream对象
         * */
        XStream xmlUtils =new XStream(new DomDriver());
//        /**
//         *设置XML节点别名为TransId,属性名 ，依据xml文件进行设定
//         * */
        xmlUtils.processAnnotations(Bocb2e.class);
//        xmlUtils.alias("bocb2e", Bocb2e.class);
//        xmlUtils.aliasAttribute(Bocb2e.class, "version", "version");
//        xmlUtils.aliasAttribute(Bocb2e.class, "locale", "locale");
//        //xmlUtils.aliasField("head", Head.class, "head");
//
//        xmlUtils.processAnnotations(Head.class);
//        xmlUtils.alias("head", Head.class);
//        xmlUtils.aliasAttribute(Head.class, "termid", "termid");
//        xmlUtils.aliasAttribute(Head.class, "trnid", "trnid");
//        xmlUtils.aliasAttribute(Head.class, "custid", "custid");
//
//        xmlUtils.aliasAttribute(Head.class, "cusopr", "cusopr");
//        xmlUtils.aliasAttribute(Head.class, "trncod", "trncod");
//        xmlUtils.aliasAttribute(Head.class, "token", "token");

        /**
         * 执行转换
         * */
        bocb2e = (Bocb2e) xmlUtils.fromXML(xmlStr);

        String toXml = xmlUtils.toXML(bocb2e);
        System.out.println("对象转换为XML:"+toXml);
    }
}
